package com.starlink.client.jd.impl;

import com.lop.open.api.sdk.DefaultDomainApiClient;
import com.lop.open.api.sdk.LopException;
import com.lop.open.api.sdk.domain.ECAP.CommonCreateOrderApi.commonCreateOrderV1.CommonCreateOrderResponse;
import com.lop.open.api.sdk.domain.ECAP.CommonModifyCancelOrderApi.commonModifyOrderV1.CommonModifyCancelOrderResponse;
import com.lop.open.api.sdk.domain.ECAP.CommonQueryOrderApi.commonGetOrderTraceV1.CommonOrderTraceResponse;
import com.lop.open.api.sdk.domain.jdcloudprint.PullDataService.pullData.PrePrintDataInfo;
import com.lop.open.api.sdk.domain.ECAP.CommonSubscribeTraceApi.commonSubscribeTraceV1.Response;
import com.lop.open.api.sdk.plugin.LopPlugin;
import com.lop.open.api.sdk.plugin.factory.OAuth2PluginFactory;
import com.lop.open.api.sdk.request.ECAP.*;
import com.lop.open.api.sdk.request.jdcloudprint.PullDataServicePullDataLopRequest;
import com.lop.open.api.sdk.response.ECAP.*;
import com.lop.open.api.sdk.response.jdcloudprint.PullDataServicePullDataLopResponse;
import com.starlink.client.jd.JdClient;
import com.starlink.client.jd.constants.JdConstants;
import com.starlink.client.jd.properties.JdProperties;
import com.starlink.client.jd.request.*;
import com.starlink.common.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class JdClientImpl implements JdClient {

    @Autowired
    private DefaultDomainApiClient jdClient;
    @Autowired
    private JdProperties jdProperties;

    @Override
    public boolean preCheck(PreCheckRequest request) {
        try {
            EcapV1OrdersPrecheckLopRequest lopRequest = new EcapV1OrdersPrecheckLopRequest();
            lopRequest.setRequest(request.toCommonCreateOrderRequest());

            LopPlugin lopPlugin = OAuth2PluginFactory.produceLopPlugin(jdProperties.getAppKey(), jdProperties.getAppSecret(), jdProperties.getAccessToken());
            lopRequest.addLopPlugin(lopPlugin);
            EcapV1OrdersPrecheckLopResponse response = jdClient.execute(lopRequest);
            AssertUtil.assertNotNull(response, "调用京东物流下单前置校验接口失败");
            AssertUtil.assertTrue(JdConstants.SUCCESS_CODE.equals(response.getCode()), "调用京东物流下单前置校验接口失败 原因：" + response.getMsg());
        } catch (LopException e) {
            throw new RuntimeException(e);
        }
        return true;
    }

    @Override
    public CommonCreateOrderResponse create(CreateOrderRequest request) {
        try {
            EcapV1OrdersCreateLopRequest lopRequest = new EcapV1OrdersCreateLopRequest();
            lopRequest.setRequest(request.toJd4Create());
            LopPlugin lopPlugin = OAuth2PluginFactory.produceLopPlugin(jdProperties.getAppKey(), jdProperties.getAppSecret(), jdProperties.getAccessToken());
            lopRequest.addLopPlugin(lopPlugin);
            EcapV1OrdersCreateLopResponse response = jdClient.execute(lopRequest);
            AssertUtil.assertNotNull(response, "调用京东物流接口下单接口失败");
            AssertUtil.assertTrue(JdConstants.SUCCESS_CODE.equals(response.getCode()), "调用京东物流接口下单接口失败 原因：" + response.getMsg());
            AssertUtil.assertNotNull(response.getResult(), "");
            AssertUtil.assertNotNull(response.getResult().getCode(), "");
            AssertUtil.assertTrue(response.getResult().getSuccess(), "调用京东物流接口下单接口失败 原因：" + response.getResult().getMsg());
            return response.getResult().getData();
        } catch (LopException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public CommonModifyCancelOrderResponse modify(ModifyOrderRequest request) {
        try {
            EcapV1OrdersModifyLopRequest lopRequest = new EcapV1OrdersModifyLopRequest();
            lopRequest.setRequest(request.toJd4Modify());
            LopPlugin lopPlugin = OAuth2PluginFactory.produceLopPlugin(jdProperties.getAppKey(), jdProperties.getAppSecret(), jdProperties.getAccessToken());
            lopRequest.addLopPlugin(lopPlugin);

            EcapV1OrdersModifyLopResponse response = jdClient.execute(lopRequest);
            AssertUtil.assertNotNull(response, "调用京东物流修改订单接口失败");
            AssertUtil.assertTrue(JdConstants.SUCCESS_CODE.equals(response.getCode()), "调用京东物流修改订单接口失败 原因：" + response.getMsg());
            AssertUtil.assertNotNull(response.getResult(), "");
            AssertUtil.assertNotNull(response.getResult().getCode(), "");
            AssertUtil.assertTrue(response.getResult().getSuccess(), "调用京东物流修改订单接口失败 原因：" + response.getResult().getMsg());
            return response.getResult().getData();
        } catch (LopException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public com.lop.open.api.sdk.domain.ECAP.CommonModifyCancelOrderApi.commonCancelOrderV1.CommonModifyCancelOrderResponse cancel(CancelOrderRequest request) {
        try {
            EcapV1OrdersCancelLopRequest lopRequest = new EcapV1OrdersCancelLopRequest();
            lopRequest.setRequest(request.toJdCancel());
            LopPlugin lopPlugin = OAuth2PluginFactory.produceLopPlugin(jdProperties.getAppKey(), jdProperties.getAppSecret(), jdProperties.getAccessToken());
            lopRequest.addLopPlugin(lopPlugin);
            EcapV1OrdersCancelLopResponse response = jdClient.execute(lopRequest);
            AssertUtil.assertNotNull(response, "调用京东物流取消下单接口失败");
            AssertUtil.assertTrue(JdConstants.SUCCESS_CODE.equals(response.getCode()), "调用京东物流取消下单接口失败 原因：" + response.getMsg());
            AssertUtil.assertNotNull(response.getResult(), "");
            AssertUtil.assertNotNull(response.getResult().getCode(), "");
            AssertUtil.assertTrue(response.getResult().getSuccess(), "调用京东物流取消下单接口失败 原因：" + response.getResult().getMsg());
            return response.getResult().getData();
        } catch (LopException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public CommonOrderTraceResponse traceQuery(TraceQueryRequest request) {
        try {
            EcapV1OrdersTraceQueryLopRequest lopRequest = new EcapV1OrdersTraceQueryLopRequest();

            lopRequest.setCommonOrderTraceRequest(request.toJd4TraceRequest());
            LopPlugin lopPlugin = OAuth2PluginFactory.produceLopPlugin(jdProperties.getAppKey(), jdProperties.getAppSecret(), jdProperties.getAccessToken());
            lopRequest.addLopPlugin(lopPlugin);
            EcapV1OrdersTraceQueryLopResponse response = jdClient.execute(lopRequest);
            AssertUtil.assertNotNull(response, "调用京东物流下单追综接口失败");
            AssertUtil.assertTrue(JdConstants.SUCCESS_CODE.equals(response.getCode()), "调用京东物流下单追综接口失败 原因：" + response.getMsg());
            AssertUtil.assertNotNull(response.getResult(), "");
            AssertUtil.assertNotNull(response.getResult().getCode(), "");
            AssertUtil.assertTrue(response.getResult().getSuccess(), "调用京东物流下单追综接口失败 原因：" + response.getResult().getMsg());
            return response.getResult().getData();
        } catch (LopException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Response<String> subscribeTrace(SubscribeTraceRequest request) {
        try {
            EcapV1OrdersTraceSubscribeLopRequest lopRequest =  new EcapV1OrdersTraceSubscribeLopRequest();
            lopRequest.setRequest(request.toSubscribeTraceRequest());
            LopPlugin lopPlugin = OAuth2PluginFactory.produceLopPlugin(jdProperties.getAppKey(), jdProperties.getAppSecret(), jdProperties.getAccessToken());
            lopRequest.addLopPlugin(lopPlugin);
            EcapV1OrdersTraceSubscribeLopResponse response = jdClient.execute(lopRequest);
            AssertUtil.assertNotNull(response, "调用物流轨迹订阅失败");
            AssertUtil.assertTrue(JdConstants.SUCCESS_CODE.equals(response.getCode()), "调用物流轨迹订阅失败 原因：" + response.getMsg());
            AssertUtil.assertNotNull(response.getResult(), "");
            AssertUtil.assertNotNull(response.getResult().getCode(), "");
            AssertUtil.assertTrue(response.getResult().getSuccess(), "调用物流轨迹订阅失败 原因：" + response.getResult().getMsg());
            return response.getResult();
        } catch (LopException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void statusGet() {

    }

    @Override
    public List<PrePrintDataInfo> pullData(PullDataRequest request) {
        try {
            PullDataServicePullDataLopRequest lopRequest = new PullDataServicePullDataLopRequest();
            LopPlugin lopPlugin = OAuth2PluginFactory.produceLopPlugin(jdProperties.getAppKey(), jdProperties.getAppSecret(), jdProperties.getAccessToken());
            lopRequest.addLopPlugin(lopPlugin);
            lopRequest.setPullDataReqDTO(request.toPullDataRequest(jdProperties.getCustomerCode()));
            PullDataServicePullDataLopResponse response = jdClient.execute(lopRequest);
            AssertUtil.assertNotNull(response, "调用获取打印数据校验失败");
            AssertUtil.assertTrue(JdConstants.SUCCESS_CODE.equals(response.getCode()), "调用获取打印数据校验失败 原因：" + response.getMsg());
            AssertUtil.assertNotNull(response.getResult(), "获取打印数据响应体为空");
            AssertUtil.assertNotNull(response.getResult().getCode(), "获取打印数据响应状态码为空");
            AssertUtil.assertTrue(JdConstants.PULL_DATA_SUCCESS_CODE.equals(response.getResult().getCode()), "调用获取打印数据校验失败 原因：" + response.getResult().getMessage());
            return response.getResult().getPrePrintDatas();
        } catch (LopException e) {
            throw new RuntimeException(e);
        }
    }
}
